<?php
/**
 * @file $HeadURL: attendanceworkcycle.php $
 * @author $Author: ZhangFan (zhangfan1@haibao.com) $
 * @date $Date: 2016-1-14 $
 * @brief
 */

namespace haibao\cms\business\attendance;

use haibao\cms\data\attendance\AttendanceWorkCycle as WorkCycleData;
use haibao\app\business\BusinessException;
class AttendanceWorkCycle extends \haibao\cms\business\Base{
    const MONDAY = 1;
    const TUESDAY = 2;
    const WEDNESDAY = 3;
    const THURSDAY = 4;
    const FRIDAY = 5;
    const SATURDAY = 6;
    const SUNDAY = 7;
    const MONDAY_KEY = 1;
    const TUESDAY_KEY = 2;
    const WEDNESDAY_KEY = 4;
    const THURSDAY_KEY = 8;
    const FRIDAY_KEY = 16;
    const SATURDAY_KEY = 32;
    const SUNDAY_KEY = 64;
    public static function addWorkCycle($model){
        self::checkOperation(self::FUNC_CODE_ATTENDANCE_WORK_CYCLE_ADD);
        if(!$model->JobNum){
            throw new BusinessException('请添加员工');
        }
        if(!$model->WeekCycle){
            throw new BusinessException('请选择工作周期');
        }
        $workCycleData = new WorkCycleData();
        $workCycleData->add($model);
        self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_WORK_CYCLE_ADD,$model);
    }
    public static function deleteWorkCycle($jobNums){
        if(!$jobNums){
            throw new BusinessException('请选择员工');
        }
        $workCycleData = new WorkCycleData();
        $workCycleData->deleteWorkCycle($jobNums);
    }
    public static function getWeek($jobNum){
        if(!$jobNum){
            throw new BusinessException('员工编号不能为空');
        }
        $daysOfWeek = array(
            self::MONDAY_KEY=>array('name'=>'星期一','week'=>self::MONDAY),
            self::TUESDAY_KEY=>array('name'=>'星期二','week'=>self::TUESDAY),
            self::WEDNESDAY_KEY=>array('name'=>'星期三','week'=>self::WEDNESDAY),
            self::THURSDAY_KEY=>array('name'=>'星期四','week'=>self::THURSDAY),
            self::FRIDAY_KEY=>array('name'=>'星期五','week'=>self::FRIDAY),
            self::SATURDAY_KEY=>array('name'=>'星期六','week'=>self::SATURDAY),
            self::SUNDAY_KEY=>array('name'=>'星期日','week'=>self::SUNDAY)
        );
        $workCycleData = new WorkCycleData();
        $weekCnt = $workCycleData->getWorkCycle($jobNum);
        if(!$weekCnt){
            return array(self::MONDAY,self::TUESDAY,self::WEDNESDAY,self::THURSDAY,self::FRIDAY);
        }
        $weekCycle = array();
        foreach($daysOfWeek as $key=>$value){
            if(($weekCnt&$key)>0){
                array_push($weekCycle,$value['week']);
            }
        }
        return $weekCycle;
    }
    public static function getWeekKey($jobNum){
    	if(!$jobNum){
    		throw new BusinessException('员工编号不能为空');
    	}
    	$daysOfWeek = array(
    			self::MONDAY_KEY=>array('name'=>'星期一','week'=>self::MONDAY),
    			self::TUESDAY_KEY=>array('name'=>'星期二','week'=>self::TUESDAY),
    			self::WEDNESDAY_KEY=>array('name'=>'星期三','week'=>self::WEDNESDAY),
    			self::THURSDAY_KEY=>array('name'=>'星期四','week'=>self::THURSDAY),
    			self::FRIDAY_KEY=>array('name'=>'星期五','week'=>self::FRIDAY),
    			self::SATURDAY_KEY=>array('name'=>'星期六','week'=>self::SATURDAY),
    			self::SUNDAY_KEY=>array('name'=>'星期日','week'=>self::SUNDAY)
    	);
    	$workCycleData = new WorkCycleData();
    	$weekCnt = $workCycleData->getWorkCycle($jobNum);
    	if(!$weekCnt){
    		return array(self::MONDAY_KEY,self::TUESDAY_KEY,self::WEDNESDAY_KEY,self::THURSDAY_KEY,self::FRIDAY_KEY);
    	}
    	$weekCycle = array();
    	foreach($daysOfWeek as $key=>$value){
    		if(($weekCnt&$key)>0){
    			array_push($weekCycle,$key);
    		}
    	}
    	return $weekCycle;
    }
    public static function getWorkCycleList($workCycleFilter){
        $workCycleData = new WorkCycleData();
        return $workCycleData->getAll($workCycleFilter);
    }
    public static function deleteWorkCycleById($workCycleId){
        self::checkOperation(self::FUNC_CODE_ATTENDANCE_WORK_CYCLE_DEL);
        if(!$workCycleId){
            throw new BusinessException('工作周期ID不能为空');
        }
        $workCycleData = new WorkCycleData();
        $workCycleData->removeById($workCycleId);
        self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_WORK_CYCLE_DEL, $workCycleId);
    }
}